package com.wx.mythread.threadmethod;


/**
 * 两个线程交替打印0-100
 * 使用wait/notify
 *
 */
public class StudyPrintWait {

    private static volatile Object lock = new Object();
    private static int count;

    public static void main(String[] args) {
        new Thread(new Print()).start();
        new Thread(new Print()).start();
    }

    static class Print implements Runnable{

        @Override
        public void run() {
            while (count <= 100){
                synchronized (lock) {
                    System.out.println(Thread.currentThread().getName() + ":" + count++);
                    lock.notify();
                    if (count <= 100) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }
    }
}
